#include "project_tree_dock.h"

#include <QVBoxLayout>

ProjectTreeDock::ProjectTreeDock(QWidget *parent) : QWidget(parent) {}

void ProjectTreeDock::Init() {
  QVBoxLayout *mainVbox = new QVBoxLayout;
  mainVbox->setContentsMargins(0, 0, 0, 0);  // 去除内部边距
  InitTop(mainVbox);
  InitTreeWidget(mainVbox);
  this->setLayout(mainVbox);
}

void ProjectTreeDock::InitTop(QVBoxLayout *mainVbox) {
  lineEdit_ = new QLineEdit(this);
  searchButton_ = new QPushButton(QIcon("path/to/search_icon.png"), "", this);

  QHBoxLayout *innerLayout = new QHBoxLayout();
  // innerLayout->addStretch();  // 添加可伸缩空间，让按钮靠右
  innerLayout->addWidget(lineEdit_);
  innerLayout->addWidget(searchButton_);
  innerLayout->setContentsMargins(0, 0, 0, 0);  // 去除内部边距
  innerLayout->setSpacing(0);                   // 去除控件间距

  mainVbox->addLayout(innerLayout);
}

void ProjectTreeDock::InitTreeWidget(QVBoxLayout *mainVbox) {
  tree_ = new ProjectTree(this);
  treeModel_ = new TreeModel(this);
  tree_->setModel(treeModel_);
  mainVbox->addWidget(tree_);

  TreeNode *root = new TreeNode({}, nullptr);

  TreeNode *root1 = new TreeNode({}, root);
  root->addChild(root1);
  TreeNode *root12 = new TreeNode({}, root1);
  root1->addChild(root12);
  TreeNode *root13 = new TreeNode({}, root12);
  root12->addChild(root13);

  TreeNode *root21 = new TreeNode({}, root);
  root->addChild(root21);
  TreeNode *root22 = new TreeNode({}, root21);
  root21->addChild(root22);
  TreeNode *root23 = new TreeNode({}, root22);
  root22->addChild(root23);

  treeModel_->InitRootNode(root);
}
